package com.haimi.shiguang.business.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.haimi.shiguang.R;
import com.haimi.shiguang.base.BaseActivity;
import com.haimi.shiguang.base.FragmentController;
import com.haimi.shiguang.business.active.ActiveFragment;
import com.haimi.shiguang.business.find.FindFragment;
import com.haimi.shiguang.business.home.HomeFragment;
import com.haimi.shiguang.business.user.center.CenterFragment;
import com.haimi.shiguang.utils.AppExitHelper;
import com.haimi.shiguang.utils.ToastUtil;

/**
 * @author changsunhaipeng
 * @date 2018/6/17
 */

public class MainActivity extends BaseActivity {

    private FrameLayout mContainer;
    private RadioGroup mRgMain;
    private ImageView mAddActive;
    private AppExitHelper appExitHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appExitHelper = new AppExitHelper(getContextWithinHost());
    }

    @Override
    public int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mContainer = findViewById(R.id.fl_main);
        mRgMain = findViewById(R.id.rg_main);
        mAddActive = findViewById(R.id.iv_add_active);
        mAddActive.setOnClickListener(this);
        HomeFragment homeFragment = HomeFragment.newInstance();
        FragmentController.addFragment(getSupportFragmentManager(), homeFragment, HomeFragment.TAG, mContainer.getId(), 0, 0);
    }

    @Override
    public void initListener() {
        mRgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        HomeFragment homeFragment = HomeFragment.newInstance();
                        FragmentController.addFragment(getSupportFragmentManager(), homeFragment, HomeFragment.TAG, mContainer.getId(), 0, 0);
                        break;
                    case R.id.rb_find:
                        FindFragment findFragment = FindFragment.newInstance();
                        FragmentController.addFragment(getSupportFragmentManager(), findFragment, FindFragment.TAG, mContainer.getId(), 0, 0);
                        break;
                    case R.id.rb_active:
                        ActiveFragment activeFragment = ActiveFragment.newInstance();
                        FragmentController.addFragment(getSupportFragmentManager(), activeFragment, ActiveFragment.TAG, mContainer.getId(), 0, 0);
                        break;
                    case R.id.rb_person_center:
                        CenterFragment personCenterFragment = CenterFragment.newInstance();
                        FragmentController.addFragment(getSupportFragmentManager(), personCenterFragment, CenterFragment.TAG, mContainer.getId(), 0, 0);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add_active:
                ToastUtil.showToast("敬请期待");
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return appExitHelper.onKeyDown(keyCode, event);
    }
}
